<!-- 调拨入库 -->
<template>
	<view class="warehouse">
		<view class="warehouse-content">
			<u-sticky bgColor="#fff">
				<view class="warehouse-cons">
					<view class="warehouse-box">
						<view class="warehouse-title">
							基本信息
						</view>
						<view class="warehouse-item">
							<view class="item-con">
								<view class="warehouse-item-title">
									单据编号：
								</view>
								<view class="warehouse-item-text">
									{{warehousingDetails.documentNumber}}
								</view>
							</view>
							<view class="item-con">
								<view class="warehouse-item-title">
									仓库：
								</view>
								<view class="warehouse-item-text">
									{{warehousingDetails.warehouseName}}
								</view>
							</view>
							<view class="item-con">
								<view class="warehouse-item-title">
									批次号：
								</view>
								<view class="warehouse-item-text">
									{{warehousingDetails.batchNumber}}
								</view>
							</view>
							<view class="item-con">
								<view class="warehouse-item-title">
									经手人：
								</view>
								<view class="warehouse-item-text">
									{{warehousingDetails.preparedBy}}
								</view>
							</view>
							<view class="item-con">
								<view class="warehouse-item-title">
									业务时间：
								</view>
								<view class="warehouse-item-text">
									{{warehousingDetails.businessTime?customFormat(warehousingDetails.businessTime):'-'}}
								</view>
							</view>
						</view>
					</view>
					<view class="tabs">
						<u-tabs :list="tabList" @change="handleTab"></u-tabs>
					</view>
				</view>
			</u-sticky>
			<view class="warehouse-list">
				<view class="">
					<view class="">
						<u-loading-page :loading="loading"></u-loading-page>
						<view class="list-box" v-if="warehousingDetails.details&&warehousingDetails.details.length>0">
							<view class="warehouse-title">
								入库货品
							</view>
							<view class="warehouse-list-item" v-for="(item, index) in warehousingDetails.details"
								:key="index">
								<view class="item-con">
									<view class="warehouse-item-title">
										货品名称：
									</view>
									<view class="warehouse-item-text">
										{{item.goodsName}}
									</view>
								</view>
								<view class="item-con">
									<view class="warehouse-item-title">
										规格型号：
									</view>
									<view class="warehouse-item-text">
										{{item.model}}
									</view>
								</view>
								<view class="item-con">
									<view class="warehouse-item-title">
										货品分类：
									</view>
									<view class="warehouse-item-text">
										{{item.goodsCategoryName}}
									</view>
								</view>
								<view class="item-con">
									<view class="warehouse-item-title">
										货号编号：
									</view>
									<view class="warehouse-item-text">
										{{item.goodsNumber}}
									</view>
								</view>
								<view class="item-con">
									<view class="warehouse-item-title">
										入库数量：
									</view>
									<view class="warehouse-item-text">
										{{item.number}}
									</view>
								</view>
								<view class="item-con">
									<view class="warehouse-item-title">
										异常数量：
									</view>
									<view class="warehouse-item-text">
										{{item.abnormalQuantity}}
									</view>
								</view>
								<view class="item-con">
									<view class="warehouse-item-title">
										单价：
									</view>
									<view class="warehouse-item-text">
										{{item.unitPrice}}
									</view>
								</view>
								<u--form labelPosition="left" :model="item" :rules="rules" ref="uForm"
									labelWidth="80px">
									<u-form-item label="实际数量：" prop="name" borderBottom ref="item1">
										<u--input v-if="!isShowScan" v-model="item.actualQuantity" type="number"
											:maxlength="item.number" @blur="actualQuantityNumber(item)"></u--input>
										<u--input v-else :disabled="true" v-model="item.alreadyInboundQuantity"
											disabledColor="#ffffff" border="none"></u--input>
									</u-form-item>
									<u-form-item label="入库库区：" prop="roles.name" borderBottom ref="item1"
										@click="handleDamAreaName(item,index)">
										<u--input v-model="item.damAreaName" disabled disabledColor="#ffffff"
											placeholder="请选择" placeholder-style="font-size: 13px"
											border="none"></u--input>
										<u-icon slot="right" name="arrow-right" size="13px"></u-icon>
									</u-form-item>
									<u-form-item label="入库巷道：" prop="roles.name" borderBottom ref="item1"
										@click="handleLaneList(item)">
										<u--input v-model="item.laneName" disabled disabledColor="#ffffff"
											placeholder="请选择" placeholder-style="font-size: 13px"
											border="none"></u--input>
										<u-icon slot="right" name="arrow-right" size="13px"></u-icon>
									</u-form-item>
									<u-form-item label="入库库位：" prop="roles.name" borderBottom ref="item1"
										@click="handleGetLocationList(item)">
										<u--input v-model="item.locationName" disabled disabledColor="#ffffff"
											placeholder="请选择" placeholder-style="font-size: 13px"
											border="none"></u--input>
										<u-icon slot="right" name="arrow-right" size="13px"></u-icon>
									</u-form-item>

									<!-- 扫码按钮 - 根据 isScan 状态显示不同内容 -->
									<u-form-item label=" " rop="roles.name" style="margin: 10px 0;"
										v-if="!isShowScan && !item.isScan">
										<u-button type="primary" :plain="true" text="扫码入库"
											@click="handleScanCode(item, index)" :loading="scanLoading === index"
											:disabled="scanLoading !== null && scanLoading !== index"></u-button>
									</u-form-item>

									<!-- 已扫码状态显示 -->
									<u-form-item label=" " rop="roles.name" style="margin: 10px 0;" v-else>
										<view class="scan-status">
											<u-icon name="checkmark-circle" color="#19be6b" size="18"></u-icon>
											<text class="scan-text">已扫码</text>
											<text class="scan-time"
												v-if="item.scanTime">{{ formatScanTime(item.scanTime) }}</text>
										</view>
									</u-form-item>
								</u--form>
							</view>
							<u-action-sheet :show="showSex1" :actions="damAreaList" title="请选择库区"
								@close="showSex1 = false" @select="damAreaSelect">
							</u-action-sheet>
							<u-action-sheet :show="showSex2" :actions="laneList" title="请选择巷道" @close="showSex2 = false"
								@select="laneSelect">
							</u-action-sheet>
							<u-action-sheet :show="showSex3" :actions="locationList" title="请选择库位"
								@close="showSex3 = false" @select="locationSelect">
							</u-action-sheet>
						</view>
						<view class="list-box" v-else>
							暂无数据
						</view>
					</view>

				</view>
			</view>
		</view>
		<view class="btn w-full fixed">
			<view class="but-con" v-if="!isShowScan">
				<u-button type="success" :plain="true" text="完成" @click="handleAddWarehousing"></u-button>
				<u-button type="primary" :plain="true" text="保存草稿" @click="handleTempWarehousing"></u-button>
			</view>
		</view>
		<u-modal @confirm="confirmTempWarehousing" @cancel="cancelTempWarehousing" :show="isTempWarehousing" title="提示"
			content='确定保存草稿？' :show-cancel-button="true" confirm-text="确定" cancel-text="取消" confirm-color="#2979ff"
			cancel-color="#606266"></u-modal>
		<u-modal @confirm="confirmAddWarehousing" @cancel="cancelAddWarehousing" :show="isAddWarehousing" title="提示"
			content='确定入库？' :show-cancel-button="true" confirm-text="确定" cancel-text="取消" confirm-color="#2979ff"
			cancel-color="#606266"></u-modal>
	</view>
</template>

<script>
	import {
		parseTime,
		formatTime,
		formatTimestamp
	} from "@/utils/ruoyi"

	import {
		getWarehousingList,
		getWarehouseListExtend,
		getLocationListExtend,
		getScanData,
		tempWarehousing,
		getTempWarehousing,
		createWarehousing
	} from "@/api/indexDetails/warehouse.js"
	import {
		handleTree
	} from "@/utils/tree.js"
	export default {
		data() {
			return {
				isShowScan: false,
				tabList: [{
					name: '待入库',
					value: "1"
				}, {
					name: '已入库',
					value: "2"
				}],
				warehousingTempDetails: "", //暂存数据
				isTempWarehousing: false,
				isAddWarehousing: false, // 是否完成入库
				warehouseTrees: [], //仓库列表
				damAreaList: [], //库区列表
				laneList: [], //巷道列表
				locationList: [], //库位列表
				warehousingDetails: {
					details: []
				},
				subId: '',
				loading: false,
				showSex1: false,
				showSex2: false,
				showSex3: false,
				currentIndex: -1, // 当前操作的货品索引
				rules: {},
				scanLoading: null, // 当前正在扫码的索引
				scanHistory: [], // 扫码历史记录，用于去重
				lastScanTime: 0, // 上次扫码时间
			};
		},
		onLoad: function(options) {
			this.subId = options.id
			this.documentNumber = options.documentNumber
			this.$nextTick(function() {
				this.handleGetTempWarehousing(this.documentNumber, this.subId)
				// this.handlegetWarehousingList()
			})
		},
		methods: {
			// 切换handleTab
			handleTab(res) {
				this.warehousingDetails.details = []
				this.isShowScan = res.value == 2
				if (res.value == 2) {
					this.handlegetWarehousingList(res.value)
				} else {
					this.handleGetTempWarehousing(this.documentNumber, this.subId, res.value)
				}
			},
			// 查询暂存是否有数据
			async handleGetTempWarehousing(documentNumber, id, tab) {
				getTempWarehousing({
					// id,
					documentNumber
				}).then(res => {
					if (res.data && res.data.holdContent != null) {
						this.warehousingTempDetails = res.data?.holdContent ? JSON.parse(res.data
							?.holdContent) : ''
						this.warehousingDetails = this.warehousingTempDetails
						this.handleWarehouseListExtend()
					} else {
						this.handlegetWarehousingList(tab ? tab : 1)
					}
				}).catch(e => {
					console.log(e, 'e');
				})
			},
			// 取消保存草稿
			cancelTempWarehousing() {
				this.isTempWarehousing = false;
			},
			cancelAddWarehousing() {
				this.isAddWarehousing = false;
			},
			// 完成入库
			confirmAddWarehousing() {
				let warehousingData = this.warehousingDetails.details.map(item => {
					return {
						goodsId: item.goodsId,
						warehousingDetailsId: item.id,
						goodsName: item.goodsName,
						warehouseId: this.warehousingDetails.warehouseId,
						warehouseName: this.warehousingDetails.warehouseName,
						damAreaId: item.damAreaId,
						damAreaName: item.damAreaName,
						laneId: item.laneId,
						laneName: item.laneName,
						locationId: item.locationId,
						locationName: item.locationNamem,
						goodsCategoryId: item.goodsCategoryId,
						goodsCategoryName: item.goodsCategoryName,
						quantity: item.number,
						batchNumber: this.warehousingDetails.batchNumber,
						purchasePrice: item.unitPrice,
						actualQuantity: item.actualQuantity,
						warehousingId: this.warehousingDetails.id,
						specificationModel: item.model,
						code: item.goodsNumber,
						unit: item.unit
					}
				})
				console.log(warehousingData, 'warehousingData');
				createWarehousing(warehousingData).then(res => {
					// 保存成功之后，返回到上一级列表并刷新列表数据
					uni.navigateBack({
						delta: 1,
						success: () => {
							// 在这里可以执行刷新列表的操作
							// uni.$emit('refreshList');
							// uni.setStorageSync('needRefresh', true);
						}
					});
				}).catch(e => {
					console.log(e, 'e');
				})
			},
			// 确定保存草稿
			confirmTempWarehousing() {
				let params = {
					holdType: 1,
					id: this.warehousingDetails.id,
					documentNumber: this.warehousingDetails.documentNumber,
					holdContent: this.warehousingDetails ? JSON.stringify(this.warehousingDetails) : ''
				}
				console.log(params, 'this.warehousingDetails');
				tempWarehousing(params).then(res => {
					// 保存成功之后，返回到上一级列表并刷新列表数据
					uni.navigateBack({
						delta: 1,
						success: () => {
							// 在这里可以执行刷新列表的操作
							// uni.$emit('refreshList');
							// uni.setStorageSync('needRefresh', true);
						}
					});
				}).catch(e => {
					console.log(e, 'e');
				})
			},
			// 发起入库
			handleAddWarehousing() {
				if (this.warehousingDetails.details.some(itm => itm.actualQuantity <= 0 && itm.isScan)) {
					uni.showToast({
						title: '入库实际数量不能为0',
						icon: 'none'
					})
					return
				}
				// 判断如果details里面有一个库区或者巷道或者库位没有填写，就直接拦截并提示填写
				if (this.warehousingDetails.details.some(itm => !itm.damAreaId || !itm.laneId || !itm.locationId)) {
					uni.showToast({
						title: '请填写库区、巷道、库位',
						icon: 'none'
					})
					return
				}
				// 判断如果details里面有一个没有扫码，就提示无法完成，还有未完成的货品扫码
				if (this.warehousingDetails.details.some(itm => !itm.isScan)) {
					uni.showToast({
						title: '还有未完成的货品扫码',
						icon: 'none'
					})
					return
				}
				// 判断如果details里面有一个实际数量小于应入库数量，就提示无法完成，还有未完成的货品扫码
				// if (this.warehousingDetails.details.some(itm => itm.actualQuantity < itm.number)) {
				// 	uni.showToast({
				// 		title: '还有未完成的货品扫码',
				// 		icon: 'none'
				// 	})
				// 	return
				// }
				this.isAddWarehousing = true
			},
			// 入库暂存
			handleTempWarehousing() {
				// 二次确认
				this.isTempWarehousing = true
				// uni.showModal({
				// 	title: '提示',
				// 	content: '确定保存草稿',
				// 	success: function(resModle) {
				// 		if (resModle.confirm) {
				// 	} else if (resModle.cancel) {
				// 		console.log('用户点击取消');
				// 	}
				// }
				// });
			},

			// 扫码入库
			async handleScanCode(item, index) {
				// 防重复点击
				if (this.scanLoading !== null) {
					return;
				}

				// 防抖处理 - 1秒内只能扫码一次
				const now = Date.now();
				if (now - this.lastScanTime < 1000) {
					uni.showToast({
						title: '扫码过于频繁，请稍后重试',
						icon: 'none'
					});
					return;
				}

				try {
					this.scanLoading = index;
					this.lastScanTime = now;

					// 显示对焦提示
					uni.showLoading({
						title: '请对准条码...',
						mask: true,
						duration: 2000
					});

					// 延迟确保摄像头对焦
					await this.delay(800);

					// 检查权限
					await this.checkCameraPermission();

					uni.scanCode({
						onlyFromCamera: true,
						scanType: ['barCode', 'qrCode'],
						autoZoom: true, // 启用自动放大
						autoDecodeCharset: true, // 自动字符集解码
						sound: 'default', // 播放提示音
						success: async (res) => {
							console.log('扫码结果:', res);

							// 验证扫码结果
							if (this.validateScanResult(res)) {
								await this.processScanResult(res, item, index);
							} else {
								uni.showToast({
									title: '条码识别失败，请重试',
									icon: 'none'
								});
							}
						},
						fail: (err) => {
							console.log('扫码失败:', err);
							let errorMsg = '扫码失败';

							if (err.errMsg && err.errMsg.includes('camera')) {
								errorMsg = '摄像头启动失败，请检查权限';
							} else if (err.errMsg && err.errMsg.includes('cancel')) {
								errorMsg = ''; // 用户取消，不显示提示
							}

							if (errorMsg) {
								uni.showToast({
									title: errorMsg,
									icon: 'none'
								});
							}
						},
						complete: () => {
							this.scanLoading = null;
							uni.hideLoading();
						}
					});

				} catch (error) {
					console.log('扫码异常:', error);
					this.scanLoading = null;
					uni.hideLoading();
					this.handlePermissionError(error);
				}
			},

			// 延迟函数
			delay(ms) {
				return new Promise(resolve => setTimeout(resolve, ms));
			},

			// 验证扫码结果
			validateScanResult(res) {
				if (!res.result || res.result.length === 0) {
					return false;
				}

				// 基础条码格式验证
				const barcode = res.result.trim();

				// EAN-13 验证 (13位数字)
				if (/^\d{13}$/.test(barcode)) {
					return this.validateEAN13(barcode);
				}

				// 其他常见条码格式验证
				if (/^\d{8}$/.test(barcode)) { // EAN-8
					return true;
				}
				if (/^\d{12}$/.test(barcode)) { // UPC-A
					return true;
				}
				if (/^[A-Za-z0-9\-]+$/.test(barcode) && barcode.length >= 6 && barcode.length <= 20) {
					return true; // Code 128, Code 39 等
				}

				return false;
			},

			// EAN-13 校验位验证
			validateEAN13(barcode) {
				if (!/^\d{13}$/.test(barcode)) return false;

				let sum = 0;
				for (let i = 0; i < 12; i++) {
					const digit = parseInt(barcode[i]);
					sum += (i % 2 === 0) ? digit : digit * 3;
				}
				const checksum = (10 - (sum % 10)) % 10;

				return checksum === parseInt(barcode[12]);
			},

			// 处理扫码结果
			async processScanResult(res, item, index) {
				try {
					uni.showLoading({
						title: '验证中...'
					});

					const barcode = res.result.trim();

					// 防重复扫描 - 2秒内相同的条码不重复处理
					if (this.isDuplicateScan(barcode)) {
						uni.showToast({
							title: '请勿重复扫描相同条码',
							icon: 'none'
						});
						return;
					}

					const response = await getScanData({
						barcode: barcode
					});

					if (response.code === 0 && response.data) {
						const data = response.data;

						// 验证货品匹配
						if (this.validateGoodsMatch(item, data)) {
							// 记录扫码历史
							this.recordScanHistory(barcode);

							// 使用 $set 确保响应式更新
							this.$set(this.warehousingDetails.details[index], 'isScan', true);
							this.$set(this.warehousingDetails.details[index], 'scanBarcode', barcode);
							this.$set(this.warehousingDetails.details[index], 'scanTime', new Date()
								.getTime());
							// 可选：自动填充实际数量
							// this.$set(this.warehousingDetails.details[index], 'actualQuantity', 1);

							uni.showToast({
								title: '扫码成功',
								icon: 'success',
								duration: 1500
							});

							console.log('更新后的货品信息:', this.warehousingDetails.details[index]);
						} else {
							uni.showToast({
								title: '货品不匹配，请扫描正确条码',
								icon: 'none',
								duration: 2000
							});
						}
					} else {
						uni.showToast({
							title: '未找到对应货品信息',
							icon: 'none'
						});
					}

				} catch (error) {
					console.error('处理扫码结果失败:', error);
					uni.showToast({
						title: '处理失败，请重试',
						icon: 'none'
					});
				} finally {
					uni.hideLoading();
				}
			},

			// 防重复扫描
			isDuplicateScan(barcode) {
				const now = Date.now();
				// 清理5秒前的记录
				this.scanHistory = this.scanHistory.filter(item => now - item.time < 5000);

				// 检查是否重复
				const isDuplicate = this.scanHistory.some(item => item.barcode === barcode);
				return isDuplicate;
			},

			// 记录扫码历史
			recordScanHistory(barcode) {
				this.scanHistory.push({
					barcode: barcode,
					time: Date.now()
				});
			},

			// 验证货品匹配
			validateGoodsMatch(item, scanData) {
				// 根据您的业务逻辑验证货品是否匹配
				return item.goodsId === scanData.id && item.goodsName === scanData.name;
			},

			// 权限检查方法
			checkCameraPermission() {
				return new Promise((resolve, reject) => {
					// 这里可以添加具体的权限检查逻辑
					// 暂时直接返回成功
					resolve(true);
				});
			},

			// 处理权限错误
			handlePermissionError(error) {
				uni.showModal({
					title: '需要摄像头权限',
					content: '请开启摄像头权限以使用扫码功能',
					success: (res) => {
						if (res.confirm) {
							// #ifdef MP-WEIXIN
							uni.openSetting();
							// #endif
							// #ifdef APP-PLUS
							plus.runtime.openURL(plus.runtime.getURL());
							// #endif
						}
					}
				});
			},

			// 格式化扫码时间
			formatScanTime(timestamp) {
				if (!timestamp) return '';
				return formatTime(timestamp, 'MM-DD HH:mm');
			},

			// 获取仓库树
			handleWarehouseListExtend() {
				getWarehouseListExtend().then(res => {
					this.warehouseTrees = handleTree(res.data)
					if (res.code == 0) {
						if (this.warehouseTrees.length > 0) {
							this.damAreaList = handleTree(res.data)?.find((item) => item.id === this
								.warehousingDetails
								.warehouseId)?.children
							// 转换数据格式，添加name字段用于显示
							this.damAreaList = this.damAreaList.map(itm => ({
								...itm,
								name: itm.warehouseName,
								id: itm.id,
								warehouseName: itm.warehouseName
							}))
							this.warehousingDetails.details = this.warehousingDetails.details.map(item =>
								({
									...item,
									damAreaName: item.damAreaName || '',
									damAreaId: item.damAreaId || '',
									laneName: item.laneName || '',
									laneId: item.laneId || '',
									locationName: item.locationName || '',
									locationId: item.locationId || '',
									actualQuantity: item.actualQuantity || 0,
									isScan: item.isScan || false,
								}))
						}
					}
				})
			},

			// 是否大于number的限制条件
			actualQuantityNumber(item) {
				// 如果异常数量等于0就用调拨数量是否大于手填数量判断，否则异常数量大于手填数量判断
				if (item.abnormalQuantity == 0 || item.abnormalQuantity == null) {
					if (item.actualQuantity > item.number) {
						uni.showToast({
							title: '实际数量不能大于出库数量',
							icon: 'none'
						})
						item.actualQuantity = '0'
						return
					}
				} else {
					if (item.actualQuantity > item.abnormalQuantity) {
						uni.showToast({
							title: '实际数量不能大于异常数量',
							icon: 'none'
						})
						item.actualQuantity = '0'
						return
					}
				}
			},

			formatTime(timestamp) {
				return formatTimestamp(timestamp, 'time')
			},

			customFormat(timestamp) {
				return formatTime(timestamp, 'YYYY-MM-DD HH:mm:ss')
			},

			async handlegetWarehousingList(flag) {
				this.loading = true
				console.log(this.subId, 'this.subId')
				await getWarehousingList({
					id: this.subId,
					flag: flag ? flag : 1
				}).then(async res => {
					this.warehousingDetails = res.data
					// 初始化details数组，确保每个item都有必要的字段
					if (this.warehousingDetails.details) {
						await this.handleWarehouseListExtend()
					}
					this.loading = false
				}).catch(err => {
					this.loading = false
				})
			},

			// 显示库区选择器
			handleDamAreaName(item,index) {
				if(this.isShowScan){
					uni.showToast({
						title: '已完成无法修改！',
						icon: 'none'
					})
					return
				}
				if (Number(item.abnormalQuantity) > 0) {
					uni.showToast({
						title: '已有入库记录，不能修改巷道',
						icon: 'none'
					})
					return
				}
				this.currentIndex = index // 保存当前操作的货品索引
				this.showSex1 = true
			},

			// 选择库区
			damAreaSelect(item) {
				console.log('选中的库区:', item)
				this.locationList = []
				this.laneList = []
				if (this.currentIndex !== -1) {
					// 更新当前货品的库区信息
					this.warehousingDetails.details[this.currentIndex].damAreaName = item.name
					this.warehousingDetails.details[this.currentIndex].damAreaId = item.id
					this.warehousingDetails.details[this.currentIndex].laneName = ""
					this.warehousingDetails.details[this.currentIndex].laneId = ""
					// 选择巷道后清空库位信息
					this.warehousingDetails.details[this.currentIndex].locationName = ''
					this.warehousingDetails.details[this.currentIndex].locationId = ''
				}
				this.laneList = this.damAreaList.find((itm) => itm.id ===
						item.id) //巷道列表
					?.children?.map(itm => {
						itm.name = itm.warehouseName
						return itm
					})
				this.showSex1 = false
				this.currentIndex = -1 // 重置索引
			},

			// 巷道选择
			laneSelect(item) {
				if (this.currentIndex !== -1) {
					// 更新当前货品的巷道信息
					this.warehousingDetails.details[this.currentIndex].laneName = item.name
					this.warehousingDetails.details[this.currentIndex].laneId = item.id
					// 选择巷道后清空库位信息
					this.warehousingDetails.details[this.currentIndex].locationName = ''
					this.warehousingDetails.details[this.currentIndex].locationId = ''
				}
				this.showSex2 = false
				this.currentIndex = -1 // 重置索引
			},

			// 选择库位
			locationSelect(item) {
				if (this.currentIndex !== -1) {
					// 更新当前货品的库位信息
					this.warehousingDetails.details[this.currentIndex].locationName = item.name
					this.warehousingDetails.details[this.currentIndex].locationId = item.id
				}
				this.showSex3 = false
				this.currentIndex = -1 // 重置索引
			},

			// 选择巷道去加载当前点击的最新的lanelist数据
			handleLaneList(item) {
				if(this.isShowScan){
					uni.showToast({
						title: '已完成无法修改！',
						icon: 'none'
					})
					return
				}
				if (Number(item.abnormalQuantity) > 0) {
					uni.showToast({
						title: '已有入库记录，不能修改巷道',
						icon: 'none'
					})
					return
				}
				this.currentIndex = this.warehousingDetails.details.findIndex(detail => detail === item);
				this.laneList = this.damAreaList.find((itm) => itm.id === item.damAreaId)?.children?.map(
					itm => {
						itm.name = itm.warehouseName
						return itm
					}) || []
				this.showSex2 = true;
			},

			// 选择库位去加载当前点击的最新的locationsList
			handleGetLocationList(item) {
				if(this.isShowScan){
					uni.showToast({
						title: '已完成无法修改！',
						icon: 'none'
					})
					return
				}
				this.currentIndex = this.warehousingDetails.details.findIndex(detail => detail === item);

				// 先检查是否已选择巷道
				if (!item.laneId) {
					uni.showToast({
						title: '请先选择巷道',
						icon: 'none'
					})
					return
				}
				// 请求接口获取当前库位列表
				this.locationList = []
				getLocationListExtend({
					warehouseId: this.warehousingDetails.warehouseId,
					laneId: item.laneId
				}).then(res => {
					if (res.code == 0) {
						// 转换数据格式，添加name字段用于显示
						this.locationList = res.data
						this.showSex3 = true;
					}
				})
			},

			change(e) {
				console.log("e:", e);
			},

			sexSelect(e) {
				this.formData.sex = e.name
				this.$refs.uForm.validateField('roles.sex')
			},

			// 刷新列表
			refreshList() {
				this.handlegetWarehousingList(1)
			},
		}
	}
</script>

<style lang="scss">
	::v-deep .u-form-item {
		.u-form-item__body {
			padding: 2px 0;

			.u-form-item__body__left__content__label {
				font-size: 13px !important;
			}
		}
	}

	.warehouse {
		background-color: #fff;

		.warehouse-content {
			width: 100%;

			.warehouse-title {
				padding: 2px;
				box-sizing: border-box;
				line-height: 30px;
				border-bottom: 2px solid #B9B9B9;
				font-weight: bold;
			}

			.item-con {
				display: flex;

				.warehouse-item-text {
					line-height: 22px;
				}
			}

			.warehouse-cons {
				position: fixed;
				width: 96%;
				margin: 0 2%;

				.warehouse-box {
					// height: 160px;
					border: 1px solid #606266;
					padding: 8px;
					box-sizing: border-box;
					border-radius: 4px;
					background-color: #F1F1F1;
					z-index: 100;

					.warehouse-item {
						margin: 4px 0;
					}
				}

				.tabs {
					background-color: #fff;
				}
			}


			.warehouse-list {
				padding-top: 210px;

				.list-box {
					background-color: #fff;
					width: 96%;
					margin: 2%;
					border: 1px solid #606266;
					padding: 8px;
					box-sizing: border-box;
					border-radius: 4px;

					.warehouse-list-item {
						margin: 20px 0;
						border-bottom: 2px solid #CCC;
					}
				}

			}
		}

		.btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 50px;
			line-height: 50px;
			text-align: center;
			z-index: 100;

			.but-con {
				display: flex;
				padding: 0 1%;
				box-sizing: border-box;

				.u-button {
					margin: 0 6px;
				}
			}
		}
	}

	.scan-status {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx 0;
	}

	.scan-text {
		color: #19be6b;
		font-size: 28rpx;
		margin: 0 10rpx;
	}

	.scan-time {
		color: #909399;
		font-size: 24rpx;
	}
</style>